package cc.wenshixin.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cc.wenshixin.entity.AdminUser;
import cc.wenshixin.entity.Customer;
import cc.wenshixin.entity.Visit;
import cc.wenshixin.service.AdminUserService;
import cc.wenshixin.service.CustomerService;
import cc.wenshixin.service.VisitService;

public class VisitAction extends ActionSupport implements ModelDriven<Visit>{
	//属性注入
	private VisitService visitService;
	private AdminUserService adminUserService;
	private CustomerService customerService;
	public void setVisitService(VisitService visitService) {
		this.visitService = visitService;
	}
	public void setAdminUserService(AdminUserService adminUserService) {
		this.adminUserService = adminUserService;
	}
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	
	//模型驱动封装
	private Visit visit = new Visit();
	@Override
	public Visit getModel() {
		return visit;
	}
	
	//跳转到添加页面
	public String toAddPage() {
		List<AdminUser> adminUserList = adminUserService.findAll();
		List<Customer> customerList = customerService.findAll();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("adminUserList", adminUserList);
		request.setAttribute("customerList", customerList);
		return "toAddPage";
	}
	
	//添加的方法
	public String add() {
		visitService.add(visit);
		return "add";
	}
	
	//显示的方法
	public String list() {
		List<Visit> visitList = visitService.findAll();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("visitList", visitList);
		return "list";
	}
	
	//删除的方法
	public String delete() {
		int id = visit.getId();
		Visit visit = visitService.findOne(id);
		if(visit != null) {
			visitService.delete(visit);
		}
		return "delete";
	}
	
	//转到修改页面
	public String toEditPage() {
		int id = visit.getId();
		Visit visit = visitService.findOne(id);
		List<AdminUser> adminUserList = adminUserService.findAll();
		List<Customer> customerList = customerService.findAll();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("visit", visit);
		request.setAttribute("adminUserList", adminUserList);
		request.setAttribute("customerList", customerList);
		return "toEditPage";
	}
	
	//修改的方法
	public String update() {
		visitService.update(visit);
		return "edit";
	}
	
	//转到查询界面
	public String toSelectPage() {
		List<Customer> customerList = customerService.findAll();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("customerList", customerList);
		return "toSelectPage";
	}
	
	//多条件查询的方法
	public String select() {
		List<Visit> visitList = visitService.selectMoreCondition(visit);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("visitList", visitList);
		return "select";
	}
}
